package org.wmine.preprocess;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Arc2D.Float;
import java.util.Iterator;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.table.DefaultTableModel;

import org.wmine.data.Attribute;

public class ZScore extends JFrame implements ActionListener
{
	DefaultTableModel dtm;
	JLabel lblAttr;
	JComboBox cmbAttr;
	JButton btnNormalize;
	
	public ZScore(String []columnNames,DefaultTableModel dtm)
	{
		
		this.dtm=dtm;
		this.setLayout(null);
		this.setBounds(200,200,350,250);
		this.setTitle("Min-Max Normalization");
		
		lblAttr=new JLabel("Select Attribute");
		lblAttr.setBounds(20,50,100,25);
		
		cmbAttr=new JComboBox();
		for(int i=0;i<columnNames.length;i++)
			cmbAttr.addItem(columnNames[i]);
		cmbAttr.setBounds(130,50,150,25);
		//cmbAttr.addActionListener(this);
		
		btnNormalize=new JButton("Normalize");
		btnNormalize.setBounds(110,105,100,25);
		btnNormalize.addActionListener(this);
		
		this.add(lblAttr);
		this.add(cmbAttr);
		this.add(btnNormalize);

	}

	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		Object []obj=new Object[this.dtm.getRowCount()];
		int index=cmbAttr.getSelectedIndex();
		
		float mean=CalculateMean(index);
		float sd=CalculateStandardDeviation(index,mean);
		
		PreprocessPanel.report.append("<br>You chose to Perform Z-Score Normalization<br>");
		PreprocessPanel.report.append("<br>Mean Value:&nbsp;&nbsp;"+mean+"&nbsp;&nbsp;&nbsp;&nbsp;Standard Deviation:&nbsp;&nbsp; "+sd);
		PreprocessPanel.report_summary.setText(PreprocessPanel.report.toString());
		PreprocessPanel.summary.repaint();
		
		for(int i=0;i<this.dtm.getRowCount();i++)
		{
			float value=java.lang.Float.parseFloat(this.dtm.getValueAt(i,index).toString());
			float normalized_value=(value-mean)/sd;
			obj[i]=normalized_value;
			PreprocessPanel.report.append("<br>"+value+" &nbsp;&nbsp;is normalized to&nbsp;&nbsp; ==>&nbsp;&nbsp;"+normalized_value);
		}
		AddColumn(obj,index);
		PreprocessPanel.report_summary.setText(PreprocessPanel.report.toString());
		PreprocessPanel.summary.repaint();
		
			
		this.dispose();
		
	}
	private void AddColumn(Object[] obj, int index) {
		// TODO Auto-generated method stub
		System.gc();
		System.runFinalization();
		this.dtm.addColumn("Normalized_"+cmbAttr.getSelectedItem(),obj);
		Vector v=new Vector();
		v=this.dtm.getDataVector();
		int cnt=this.dtm.getColumnCount();
		//System.out.println("v:"+v+"  :: cnt:: "+cnt);
		Vector v1=new Vector(cnt);
		for(int ii=0;ii<cnt;ii++)
			v1.add(this.dtm.getColumnName(ii));
		System.out.println("v1::"+v1);
		v1.removeElementAt(index);
		System.out.println("v1::"+v1);
		
		Iterator itr=v.iterator();
		while(itr.hasNext())
		{
			Vector o=(Vector)itr.next();
			o.removeElementAt(index);
			System.out.println("o::"+o);
		}
		this.dtm.setDataVector(v,v1);
	}

	private float CalculateMean(int index)
	{
		float sum=0,mean=0;
		
			for(int k=0;k<this.dtm.getRowCount();k++)
			{	
				sum+=java.lang.Float.parseFloat(this.dtm.getValueAt(k,index).toString());
				PreprocessPanel.report.append(dtm.getValueAt(k,index).toString()+" , ");
			}
			mean=sum/this.dtm.getRowCount();
			System.out.println(" mean:"+mean);
			
			
		return mean;
	}
	
	private float CalculateStandardDeviation(int index,float mean)
	{
		float sd=0;
		float sum=0;
		for(int i=0;i<this.dtm.getRowCount();i++)
		{
			float val=java.lang.Float.parseFloat(this.dtm.getValueAt(i,index).toString());
			double value=Math.pow((val-mean),2);
			sum+=value;
		}
		sd=sum/this.dtm.getRowCount();
		return sd;
	}
	
	public String getSelectedColumn()
	{
		return cmbAttr.getSelectedItem().toString();
	}
}